package component.userInformation;

import lombok.Data;
import protobuf.protoClass.ProtoMsg;

/**
 * 聊天信息
 *
 * @see ProtoMsg.MessageRequest
 */

@Data
public class ChatMsg {

    private UserAttribute userAttribute;

    /**
     * 对应 {@link ProtoMsg.MessageRequest}序列化
     */
    private long msgId;    //用户 ID
    private String from;   //信息发送方
    private String to;     //信息接收方
    private long time;     //发送时间
    private MessageType msgType; //信息类型
    private String content;      //信息内容
    private String url;          //多媒体地址
    private String property;     //附加属性
    private String fromUser;     //发送方用户名
    private String json;         //附加的json串

    public ChatMsg(UserAttribute userAttribute) {
        if (userAttribute == null) {
            return;
        }
        this.userAttribute = userAttribute;
        this.setTime(System.currentTimeMillis());
        this.setFrom(userAttribute.getUid());
        this.setFromUser(userAttribute.getUserName());
    }

    public static boolean isNotEmpty(CharSequence cs) {
        return !isEmpty(cs);
    }

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    /**
     * 通过 ChatMsg类设置 {@link ProtoMsg.MessageRequest} 聊天信息
     */
    public void setProtoRequestMsg(ProtoMsg.MessageRequest.Builder builder) {
        if (msgId > 0) {
            builder.setMsgId(msgId);
        }
        if (isNotEmpty(from)) {
            builder.setFrom(from);
        }
        if (isNotEmpty(to)) {
            builder.setTo(to);
        }
        if (time > 0) {
            builder.setTime(time);
        }
        if (msgType != null) {
            builder.setMsgType(msgType.ordinal());
        }
        if (isNotEmpty(content)) {
            builder.setContent(content);
        }
        if (isNotEmpty(url)) {
            builder.setUrl(url);
        }
        if (isNotEmpty(property)) {
            builder.setProperty(property);
        }
        if (isNotEmpty(fromUser)) {
            builder.setFromUser(fromUser);
        }
        if (isNotEmpty(json)) {
            builder.setJson(json);
        }
    }

    /**
     * 消息类型  1:纯文本 2:音频 3:视频 4:地理位置 5:其他
     */
    public enum MessageType {
        TEXT, AUDIO, VIDEO, POSITION, OTHER;
    }

}
